//
//  PicClassVC.m
//  OpenEyes
//
//  Created by dllo on 15/9/11.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "PicClassVC.h"
#import "PicClassModel.h"
#import "PicClassCollViewCell.h"
#import "PicDetailVC.h"
@interface PicClassVC () <UICollectionViewDataSource, UICollectionViewDelegate>

/**
 *  保存plist分类数组数据
 */
@property (nonatomic, strong) NSArray *array;

/**
 *  保存模型对象数组
 */
@property (nonatomic, strong) NSMutableArray *arrayM;

/**
 *  图片分类CollView
 */
@property(nonatomic, strong)UICollectionView *picClassCollView;


@end

@implementation PicClassVC


-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}

//重写array的getter方法
-(NSArray *)array
{
    if (_array == nil) {
        
        NSString *path = [[NSBundle mainBundle]pathForResource:@"PicClass" ofType:@".plist"];
        _array = [NSArray arrayWithContentsOfFile:path];
        
        self.arrayM = [NSMutableArray array];
        
        for (NSMutableDictionary *dic in _array) {
            
            PicClassModel *picClassModel = [[PicClassModel alloc]init];
            [picClassModel setValuesForKeysWithDictionary:dic];
            
            [self.arrayM addObject:picClassModel];
            _array = self.arrayM;
        }
    }
    return _array;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"分类";
    [self createPicClassView];
}



- (void)createPicClassView
{
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(WIDTH / 2 - 2, WIDTH / 2 - 2);
    flow.minimumInteritemSpacing = 4;
    flow.minimumLineSpacing = 4;
    self.picClassCollView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flow];
//    self.picClassCollView.bounces = NO;
    self.picClassCollView.showsVerticalScrollIndicator = NO;
    self.picClassCollView.backgroundColor = [UIColor clearColor];
    self.picClassCollView.delegate = self;
    self.picClassCollView.dataSource = self;
    [self.picClassCollView registerClass:[PicClassCollViewCell class] forCellWithReuseIdentifier:PicClassReuse];
    [self.view addSubview:self.picClassCollView];
    
    
   
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"fanhui-3.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(left)];
    
    [self buttonCreate];
    
}
-(void)left{
    [self.navigationController popViewControllerAnimated:YES];
}


// 创建跳转到分类页按钮
-(void)buttonCreate
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(W * 20, H * 550, 30, 30);
    [button setImage:[UIImage imageNamed:@"iconfont-guanbi-2"] forState:UIControlStateNormal];
    button.alpha = 0.8;
    button.layer.masksToBounds = YES;
    button.layer.borderColor = [[UIColor whiteColor] CGColor];
    [button addTarget:self action:@selector(left) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
}

#pragma mark -
#pragma mark 协议方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.array.count;
   
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PicClassCollViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PicClassReuse forIndexPath:indexPath];
    PicClassModel *model = self.array[indexPath.row];
    cell.backImage.image = [UIImage imageNamed:model.img];
    cell.titleLabel.text = model.lbl;
    return cell;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PicDetailVC *detailVC = [[PicDetailVC alloc] init];
    
    PicClassModel *model = self.array[indexPath.row];
    detailVC.getCategory = model.lbl;
    //    dailyVc.modalTransitionStyle = 2;
    //    [self presentViewController:dailyVc animated:YES completion:^{
    
    //    }];
    [self.navigationController pushViewController:detailVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
